<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<!DOCTYPE html>
<html lang="en">
<head>
	<%@ include file="/common/common-css.jsp" %>
	<script>
			cmdList = "/activitymodule/modelList.action";
	</script>
<style>
	#jquery-table td{cursor:pointer !important}
	.load_bg{background:#000;opacity:0.1;left:0px;top:0px;position:fixed;height:100%;width:100%;overflow:hidden;z-index:10000;}
	.load_img{left:50%;top:180px;position:fixed;height:50px;overflow:hidden;z-index:20000;}
</style>
<link rel="stylesheet" href="${ctx}/ace/css/just-tip.css"/>
</head>
<body class="no-skin">

<div  class="load_bg" style="display:none"></div>
<img src="http://img.xmiles.cn/commentloading.gif" class="load_img" style="display:none"/>
	<div class="page-content">
		<form id="queryForm" action="${cmdList}">
			<div class="page-content-area">
				<div class="row">
					 <div class="col-sm-12">
						<div class="widget-box" style="border:0px solid #ccc; border-top:none;border-bottom:none;margin-top:15px;">
						<hr style="margin-top:0px;margin-bottom:0x">
						<div class="widget-header widget-header-blue widget-header-flat">
								<h4 class="widget-title">模板管理</h4>
								<div class="widget-toolbar">
									<a href="#" data-action="collapse">
										<i class="ace-icon fa fa-chevron-up"></i>
									</a>
								</div>	
							</div>
							
							<div class=" widget-body">
								<!-- <div class="widget-main" style="padding:12px;border:0px solid #ccc; border-top:none;border-bottom:none;"> -->
								<div class="widget-main" >
								<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
									<label class="col-sm-6 control-label" style="text-align:right" for="modelName">名称: </label>
									<div class="col-sm-6">
									    <input class="form-control" type="text" name="modelName" id="modelName"/>
									</div>
								</div>
								
								<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
									<label class="col-sm-6 control-label" style="text-align:right" for="modelType">模板类型: </label>
									<div class="col-sm-6">
									     <select name="modelType">
										   <option  value="">全部</option>
										   <option value="0">贷款活动</option>
										   <option value="1">信用卡活动</option>
										   <option value="2">单图文模板</option>
										 </select>
									</div>
								</div>
								</div><!-- /.widget-main -->
							</div><!-- /.widget-body -->
						</div><!-- /.widget-box -->
					</div><!-- /.col-sm-12 -->
					
					<div class="col-md-12 center">
						<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3" style="text-align:right">
							<button class="btn btn-purple  no-border" type="button" name="btn-query" onclick="query_any()">
									<i class="ace-icon fa fa-search icon-on-right bigger-110"></i>
										查询
							</button>
							&nbsp;&nbsp;&nbsp;
							<button class="btn btn-grey  no-border" type="reset" onclick="Javascript:doAddPro();">
									<i class="ace-icon fa fa-undo icon-on-right bigger-110"></i>
										新增
							</button>
						</div>
					</div>						  
				</div><!-- /.row -->
				<hr style="display:none;" id="delete_hr">
					<div class="row">
						<div class="col-xs-12">
							<table id="jquery-table" class="table table-striped table-bordered table-hover">
							</table>
					</div><!-- /.col -->
				</div><!-- /.row -->
			</div><!-- /.page-content-area -->
		</form>
	</div><!-- /.page-content -->
	<%@ include file="/common/common-js.jsp" %>

	
<script type="text/javascript" src="${ctx}/js/call_func.js"></script>
<script src="${ctx}/ace/js/justTools.js"></script>
<script type="text/javascript">
 
   //【查询】按钮Click事件
   function query_any(){
		$("#output").removeAttr("style");
		$(".load_bg").removeAttr("style");
		$(".load_img").removeAttr("style");
		if(jqTable != null){
			jqTable.fnDraw();
		}else{
			loaddata();
		}
		 $("#delete_btn").attr("style","display:block");
		 $("#delete_hr").attr("style","margin-top:0px;margin-bottom:20px;display:block");
    }
 
    var jqTable =null;
	var loaddata = function(){
		jQuery(function($) {
			jqTable = $('#jquery-table').dataTable( {
             //数据列控制（包括对应返回JSON属性名称，对应表头列索引位置、数据转换封装等）
			    "columnDefs": [
			    	{	  
			    		  "title":"活动id",
			    	  	  "targets": [0],
			    	  	  "data": "id",
			    	  	  "name": "id",   //JSON返回属性
			    	  	  "bSortable": false,
			    	},
			    	{
			    		  "title":"模板名称",
			    		  "name":"modelName",
			    		  "data": "modelName",
			    	  	  "targets": [1], 
			    	  	  "bSortable": false,
						  "render": function(value, type, row) { // 返回自定义内容
							  return '<textarea class="form-control backup-name" row="3" data-id='+row.id+' style="height:100% !important;">'+value+'</textarea>';						 
						  },
			    	},
			    	{
			    		  "title":"时间",
			    		  "name":"ctime",
			    		  "data": "ctime",
			    	  	  "targets": [2], 
			    	  	  "bSortable": false,
			    	},
			    	{
			    		  "title":"类型",
			    		  "name":"modelType",
			    		  "data": "modelType",
			    	  	  "targets": [3], 
			    	  	  "bSortable": false,
			    	  	  "render": function(value, type, row) { // 返回自定义内容
								if(value==0){
									return "贷款活动模板";											
								}else if (value==1){
									return "信用卡活动模板";
								}
						  },
			    	},
			    	{
			    		  "title":"备注",
			    		  "name":"modelRemark",
			    		  "data": "modelRemark",
			    	  	  "targets": [4], 
			    	  	  "bSortable": false,
						  "render": function(value, type, row) { // 返回自定义内容
							  return '<textarea class="form-control backup-text" row="3" data-id='+row.id+' style="height:100% !important;">'+value+'</textarea>';						 
						  },
			    	},
			    	{
			    	 	  "title":"操作",
			    	 	  "name":"id",
					 	  "data": "id",
			    	  	  "targets": [5] ,
			    	  	  "bSortable": false,
						   "render": function(value, type, row) { // 返回自定义内容
							   var text="";
						   		text="<a class='btn btn-success btn-sm' href='javascript:void(0)' onclick='opstatus(" + row.id + ",\"" + row.demo +"\")'>编辑内容</a>";
						   		if(row.id!="1" && row.depth=="0"){
						   			text+="  <a class='btn btn btn-sm' href='javascript:void(0)' onclick='copyModel(" + row.id + ",\"" + row.demo +"\")'>复制活动</a>"
						   			if(row.status=="1"){
						   				text+="  <a class='btn btn btn-sm' href='javascript:void(0)' onclick='updateModel(" + row.id + ",\"" + 'del' +"\")'>下线活动</a>"
						   			}else{
						   				text+="  <a class='btn btn btn-sm' href='javascript:void(0)' onclick='updateModel(" + row.id + ",\"" + 'online' +"\")'>上线活动</a>"
						   			}
						   			text+="  <a class='btn btn btn-sm' href='javascript:void(0)' onclick='copytotest(" + row.id + ")'>复制到测试</a>"
						   		}
							   return text;
				 		 	},
			    	},
           	],
			"sAjaxSource": ctx + cmdList + "?rand=" + Math.random(),//数据源URL
			"fnServerData": function ( sSource, aoData, fnCallback ) { //从服务器请求数据
		   			var data = $('#queryForm').serializeObject();
					data.aoData = JSON.stringify(aoData);
					$.ajax({
						"dataType": 'json',
						"type": "POST",
						"url": sSource,
						"data":data,
						"success": function(resp){
							fnCallback(resp);
							initListPage();
							$(".load_bg").attr("style","display:none");
							$(".load_img").attr("style","display:none");
						},
						"error": function(XMLHttpRequest, textStatus, errorThrown) {
							$(".load_bg").attr("style","display:none");
							$(".load_img").attr("style","display:none");
							alert('请求服务器错误,请刷新重试！！');//错误提示，包括服务器重启导致的session失效
						},
					});
				},
				"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
					//console.log("---" + nRow + "+++++" + aData["id"]);
					//=====================这里实现行双击事件======================//
					nRow.ondblclick = function() {
					}; 
				},
				"sScrollXInner": "100%",
				"sScrollY": "100%",//如果不设置，IE会有问题
				 "aaSorting": [[0,"desc"]] //初始化加载，默认按第几列排序、排序方式（列从0开始计算）
		    });
	    });
    }
	
	function opstatus(id,memo){
		//var url="/module/moduleContent.jsp?id="+id+"&type="+id;
		var url="/module/activitymodule/add.jsp?id="+id;
		openDialogWithRand(url,1600,1000,"编辑模板",function (data){});
	}
	
	function copyModel(id,memo){
		var url=ctx + "/module/copyModel.action";
			 $.ajax({
				"url":url ,
				"type": "POST",
				"dataType": 'json',
				"data":  {"model_id":id},
				"beforeSend": function(){
					
				},
				"success": function(data){
					alert("操作成功");
					query_any();
				},
				"error": function(){
					alert("操作失败");								 
				},
				"complete": function(){
					//解除事件
					
				}
			}); 
		
	}
	
	function updateModel(id,operation){
		var url=ctx + "/module/delModel.action";
		 $.ajax({
			"url":url ,
			"type": "POST",
			"dataType": 'json',
			"data":  {"id":id,"operation":operation},
			"beforeSend": function(){
				
			},
			"success": function(data){
				alert("操作成功");
				query_any();
			},
			"error": function(){
				alert("操作失败");								 
			},
			"complete": function(){
				//解除事件
				
			}
		}); 
	}
	
	function copytotest(id){
		var url=ctx + "/module/copytotest.action";
		 $.ajax({
			"url":url ,
			"type": "POST",
			"dataType": 'json',
			"data":  {"id":id},
			"beforeSend": function(){
				
			},
			"success": function(data){
				alert("操作成功");
				query_any();
			},
			"error": function(){
				alert("操作失败");								 
			},
			"complete": function(){
				//解除事件
				
			}
		}); 
	}
	
	function doAddPro() {	
		/* var url = "//module/detail.action?id=-1";
		url = encodeURI(url); */
		openDialogWithRand("/module/activitymodule/add.jsp",1400,1000,"新增模板",function (data){});
	}
	
	jQuery(function(){
		$('#jquery-table').delegate('textarea.backup-text','blur',function(){
			var _this = $(this);
			var id = _this.attr('data-id');
			var val = _this.val();
			var data = "id=" + id + "&backup=" + val;
			$.ajax({
				"type": "POST",
				"dataType": 'json',
				"url": ctx + '/module/updateBgComments.action',
				"data": data,
				"timeout":300000,
				"success": function(data){
					backupTextJustToolsTip(_this,'备注成功','tooltip-suc');
				},
				"error": function(){
					backupTextJustToolsTip(_this,'备注失败','tooltip-fail');
				}
			});			
			return false;
		});
		
		$('#jquery-table').delegate('textarea.backup-name','blur',function(){
			var _this = $(this);
			var id = _this.attr('data-id');
			var val = _this.val();
			var data = "id=" + id + "&name=" + val;
			$.ajax({
				"type": "POST",
				"dataType": 'json',
				"url": ctx + '/module/updateBgComments.action',
				"data": data,
				"timeout":300000,
				"success": function(data){
					backupTextJustToolsTip(_this,'修改成功','tooltip-suc');
				},
				"error": function(){
					backupTextJustToolsTip(_this,'修改失败','tooltip-fail');
				}
			});			
			return false;
		});

	});
	
	function backupTextJustToolsTip(dom,content,theme){

		dom.justToolsTip({
			animation:"moveInLeft",
			contents:content,
			theme:theme,
			gravity:'left'
		});
		setTimeout(function(){
			$('.just-tooltip').remove();
		},3000); 
	};
</script>
</body>
</html>
